package com.example.healthmanagement.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.example.healthmanagement.R;
import com.example.healthmanagement.adapter.BindAdapter;
import com.example.healthmanagement.bean.User;
import com.example.healthmanagement.databinding.ActivityManagerUserBinding;
import com.example.healthmanagement.databinding.ItemUserBinding;
import com.example.healthmanagement.util.MySqliteOpenHelper;

public class ManagerUserActivity extends AppCompatActivity {
    private ActivityManagerUserBinding binding;
    private MySqliteOpenHelper helper;
    private BindAdapter<ItemUserBinding, User> adapter = new BindAdapter<ItemUserBinding, User>() {
        @Override
        public ItemUserBinding createHolder(ViewGroup parent) {
            return ItemUserBinding.inflate(getLayoutInflater(), parent, false);
        }

        @Override
        public void bind(ItemUserBinding itemUserBinding, User user, int position) {
            Glide.with(itemUserBinding.ivFace).load(user.getPhoto()).into(itemUserBinding.ivFace);
            itemUserBinding.tvName.setText(user.getName());
            itemUserBinding.tvAccount.setText(user.getAccount());
            itemUserBinding.tvAddress.setText(user.getAddress());
            itemUserBinding.tvPhone.setText(user.getPhone());
            itemUserBinding.tvSex.setText(user.getSex());
            itemUserBinding.getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new AlertDialog.Builder(ManagerUserActivity.this).setItems(new CharSequence[]{"编辑", "删除"}, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (which == 0) {
                                startActivity(new Intent(ManagerUserActivity.this, PersonActivity.class)
                                        .putExtra("isManager", true)
                                        .putExtra("userid", user.getId()));
                            } else if (which == 1) {
                                SQLiteDatabase writableDatabase = helper.getWritableDatabase();
                                writableDatabase.delete("user", "id = ?", new String[]{user.getId() + ""});
                                refresh();
                            }
                        }
                    }).show();

                }
            });
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityManagerUserBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        helper = new MySqliteOpenHelper(this);
        binding.rvUser.setAdapter(adapter);
        binding.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ManagerUserActivity.this, RegisterActivity.class)
                        .putExtra("isManager", true)

                );
            }
        });
    }

    public void back(View view) {
        finish();
    }

    @SuppressLint("Range")
    public void refresh() {
        adapter.getData().clear();
        SQLiteDatabase writableDatabase = helper.getWritableDatabase();
        Cursor cursor = writableDatabase.rawQuery("select * from user", null);
        if (cursor.getCount() != 0) {
            while (cursor.moveToNext()) {
                adapter.getData().add(new User(
                        cursor.getInt(cursor.getColumnIndex("id")),
                        cursor.getString(cursor.getColumnIndex("account")),
                        cursor.getString(cursor.getColumnIndex("password")),
                        cursor.getString(cursor.getColumnIndex("name")),
                        cursor.getString(cursor.getColumnIndex("sex")),
                        cursor.getString(cursor.getColumnIndex("phone")),
                        cursor.getString(cursor.getColumnIndex("address")),
                        cursor.getString(cursor.getColumnIndex("photo"))
                ));
            }
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }
}